package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RTreeCellRenderer extends DefaultTreeCellRenderer
{
	private static final long serialVersionUID = 6964075528385204058L;
	private Font font;

	public RTreeCellRenderer()
	{

	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus)
	{
		Font treeFont = tree.getFont();
		if (font != treeFont && treeFont != null)
		{
			setFont(treeFont);
			font = treeFont;
		}
		return super.getTreeCellRendererComponent(tree, value, sel, expanded,
				leaf, row, hasFocus);
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		Composite composite = ComponentTools.applyEnableAlpha((Graphics2D) g,
				isEnabled(), 1f, 0.3f);
		if (selected)
		{
			int imageOffset = 0;
			g.setColor(RThemeColor.darkThemeColor);
			if (getComponentOrientation().isLeftToRight())
				g.fillRect(imageOffset, 0, getWidth() - imageOffset,
						getHeight());
			else
				g.fillRect(0, 0, getWidth() - imageOffset, getHeight());
		}
		super.paintComponent(g);
		if (composite != null)
			((Graphics2D) g).setComposite(composite);
	}

	@Override
	public Color getTextSelectionColor()
	{
		return RThemeColor.selectedTextColor;
	}

	@Override
	public Color getTextNonSelectionColor()
	{
		return RThemeColor.colorSet.textColor;
	}

	@Override
	public Color getBackgroundSelectionColor()
	{
		return null;
	}

	@Override
	public Color getBackgroundNonSelectionColor()
	{
		return null;
	}

	@Override
	public Color getBorderSelectionColor()
	{
		return null;
	}

	@Override
	public Color getBackground()
	{
		return null;
	}

	int getLabelStart()
	{
		Icon currentI = getIcon();
		if (currentI != null && getText() != null)
			return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
		return 0;
	}
}
